package driver

import (
	"fmt"

	"github.com/edgexfoundry/device-sdk-go/v3/pkg/models"
	"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
)

type spinningDevice struct {
	resourceInt *resourceInt
}

func (d *spinningDevice) read(deviceName, deviceResourceName, typeName string, minimum, maximum *float64, db *db) (*models.CommandValue, error) {
	result := &models.CommandValue{}
	switch typeName {
	case common.ValueTypeInt8, common.ValueTypeInt16, common.ValueTypeInt32, common.ValueTypeInt64:
		return d.resourceInt.value(db, deviceName, deviceResourceName, minimum, maximum)
	default:
		return result, fmt.Errorf("spinningDevice.read: wrong read type: %s", deviceResourceName)
	}
}

func (d *spinningDevice) write(param *models.CommandValue, deviceName string, db *db) error {
	switch param.Type {
	case common.ValueTypeInt8, common.ValueTypeInt16, common.ValueTypeInt32, common.ValueTypeInt64:
		return d.resourceInt.write(param, deviceName, db)
	default:
		return fmt.Errorf("SpinningDriver.HandleWriteCommands: there is no matched device resource for %s", param.String())
	}
}

func newSpinningDevice() *spinningDevice {
	return &spinningDevice{
		resourceInt: &resourceInt{},
	}
}
